package com.pzsh.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.pzsh.aliyun.api.gateway.demo.util.HttpUtils;
import com.pzsh.constant.ConstantInterface;
import com.pzsh.constant.EOLINKERConstants;
import com.pzsh.entity.*;
import com.pzsh.entity.vo.SpecialVo;
import com.pzsh.entity.vo.TransactionVo;
import com.pzsh.entity.vo.UserPackVo;
import com.pzsh.enums.ErrorCodeEnum;
import com.pzsh.exception.ApiException;
import com.pzsh.jwt.ITokenManager;
import com.pzsh.mapper.*;
import com.pzsh.redis.IRedisService;
import com.pzsh.service.IIntegralService;
import com.pzsh.sms.aliyun.SendSms;
import com.pzsh.util.BigDecimalUtil;
import com.pzsh.util.MD5Util;
import com.pzsh.util.PageResult;
import com.pzsh.util.RowBoundUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * @Description:
 * @Param:
 * @return:
 * @Author: GXL
 * @Date: 2019/12/09
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class IntegralServiceImpl implements IIntegralService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IRedisService redisService;

    @Autowired
    private ITokenManager tokenManager;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserPackMapper userPackMapper;


    @Autowired
    private PSTransferMapper psTransferMapper;


    @Autowired
    private TransactionssMapper transactionMapper;

    @Autowired
    private SpecialMapper specialMapper;

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public UserPackVo selectIntegralByAccount(String account) {
        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        UserPackEntity userPackEntity = new UserPackEntity();
        userPackEntity.setUserId(userEntity.getUserId());
        userPackEntity = userPackMapper.selectOne(userPackEntity);


        if (userPackEntity == null){
            userPackEntity = new UserPackEntity();
            userPackEntity.setUserId(userEntity.getUserId());
            userPackEntity.setFreeIntegral(0);
            userPackEntity.setRewardIntegral(0);
            userPackEntity.setEUSDT(0);
            userPackEntity.setPZSH(0);
            userPackEntity.setRdIntegral(0);
            userPackEntity.setUnuseIntegral(0);
            userPackMapper.insert(userPackEntity);
        }

        //请求RD积分系统接口--获取会员信息
        Map<String, String> headers = new HashMap<String, String>();
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        querys.put("username", userEntity.getAccount());
        Integer wallet = 0;
        String msg = "RD系统请求异常";
        try {
            HttpResponse response = HttpUtils.doGet(EOLINKERConstants.DOMAIN_URL,EOLINKERConstants.USER_INFO, RequestMethod.GET.name(),headers,querys);
            String str = EntityUtils.toString(response.getEntity());
//            log.info("会员信息返回：{}",str);
            JSONObject jsonObject = JSONObject.parseObject(str);
            Integer status = jsonObject.getInteger("status");
            msg = jsonObject.getString("msg");
            if (status.equals(0)){
                wallet = (int)(jsonObject.getJSONObject("data").getDouble("wallet_2") * 100);
            }else{
                log.error("RD钱包余额请求失败--1");
//                throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(), msg));

            }
            userPackEntity.setRdIntegral(wallet);
            userPackMapper.updateByPrimaryKey(userPackEntity);
        } catch (Exception e) {
            log.error("RD钱包余额请求失败--2");
//            throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(), msg));
        }

        SpecialEntity specialEntity = new SpecialEntity();
        specialEntity.setSpKey("rd2ps");
        String fee = specialMapper.selectOne(specialEntity).getSpValue();
        BigDecimal rd2ps = new BigDecimal(fee);
        BigDecimal eusdt = new BigDecimal(userPackEntity.getEUSDT().toString());
        BigDecimal freeInte = new BigDecimal(userPackEntity.getFreeIntegral().toString());
        BigDecimal pzsh = new BigDecimal(userPackEntity.getPZSH().toString());
        BigDecimal rd = new BigDecimal(userPackEntity.getRdIntegral().toString());
        BigDecimal unuseInte = new BigDecimal(userPackEntity.getUnuseIntegral().toString());
        BigDecimal reward = new BigDecimal(userPackEntity.getRewardIntegral().toString());
        BigDecimal oneh = new BigDecimal(100);

        UserPackVo userPackVo = new UserPackVo();
        userPackVo.setEUSDT(String.valueOf(eusdt.divide(oneh, 2, RoundingMode.HALF_UP)));
        userPackVo.setFreeIntegral(String.valueOf(freeInte.divide(oneh, 2, RoundingMode.HALF_UP)));
        userPackVo.setPackId(userPackEntity.getPackId());
        userPackVo.setPZSH(String.valueOf(pzsh.divide(oneh, 2, RoundingMode.HALF_UP)));
        userPackVo.setRdIntegral(String.valueOf(rd.divide(oneh, 2, RoundingMode.HALF_UP)));
        userPackVo.setRewardIntegral(String.valueOf(reward.divide(oneh, 2, RoundingMode.HALF_UP)));
        userPackVo.setUnuseIntegral(String.valueOf(unuseInte.divide(oneh, 2, RoundingMode.HALF_UP)));
        userPackVo.setRdIntegralFee(fee);

        userPackVo.setUserId(userPackEntity.getUserId());

        return userPackVo;
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void rewardPsTransfer(String account, String mobile, Integer numb, String payPwd, String vcode) {
        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        if (!redisService.hasKey(ConstantInterface.VERIFICATION_CODE_FOR_PS + userEntity.getMobile()) || !vcode.equals(redisService.getString(ConstantInterface.VERIFICATION_CODE_FOR_PS + userEntity.getMobile()))){
            throw new ApiException(ErrorCodeEnum.TK1015.code(),ErrorCodeEnum.TK1015.msg());//转ps需要短信验证
        }

        payPwd = MD5Util.MD5Encode(payPwd,"utf-8");

        if (!payPwd.equals(userEntity.getPayPwd())){
            throw  new ApiException(ErrorCodeEnum.SW52.code(),ErrorCodeEnum.SW52.msg());
        }

        if (mobile.equals(userEntity.getMobile())){
            throw  new ApiException(ErrorCodeEnum.SW54.code(),ErrorCodeEnum.SW54.msg());
        }

        //扣减 申请转账用户
        UserPackEntity frompack = new UserPackEntity();
        frompack.setUserId(userEntity.getUserId());
        frompack = userPackMapper.selectOne(frompack);
        if (numb > frompack.getRewardIntegral()){
            throw new ApiException(ErrorCodeEnum.SW302.code(),ErrorCodeEnum.SW302.msg());
        }
        frompack.setRewardIntegral(frompack.getRewardIntegral() - numb);
        userPackMapper.updateByPrimaryKey(frompack);

        //扣减手续费
//        SpecialEntity specialEntity = new SpecialEntity();
//        specialEntity.setSpKey("minerFee");
//        specialEntity = specialMapper.selectOne(specialEntity);

//        BigDecimal bdnumb = new BigDecimal(numb.toString());
//        BigDecimal spv = new BigDecimal(specialEntity.getSpValue());
//        BigDecimal oneh = new BigDecimal(100);
//        BigDecimal B3 = spv.divide(oneh, 2, RoundingMode.HALF_UP);//扣除比例
//        BigDecimal B4 = bdnumb.divide(oneh,2,RoundingMode.HALF_UP);//被扣额度
//        BigDecimal B5 = B4.multiply(B3).setScale(2,BigDecimal.ROUND_HALF_UP);//扣除额度
//        BigDecimal B6 = B4.subtract(B5).setScale(2,BigDecimal.ROUND_HALF_UP).multiply(oneh);//扣除后剩余额度 * 100
//        Integer B7 = B6.intValue();
//        log.info("扣除比例：{}，额度：{}，扣除额度：{}，扣除后剩余额度(*100):{}, 取intValue：{}",B3, B4, B5, B6,B7);

        //增加 被转账用户 的ps
        UserEntity toUser = new UserEntity();
        toUser.setMobile(mobile);
        toUser = userMapper.selectOne(toUser);
        if(toUser == null){
            throw new ApiException(ErrorCodeEnum.PARAM_200026);
        }
        UserPackEntity topack = new UserPackEntity();
        topack.setUserId(toUser.getUserId());
        topack = userPackMapper.selectOne(topack);
        topack.setRewardIntegral(topack.getRewardIntegral() + numb);
        userPackMapper.updateByPrimaryKey(topack);

        //**消费积分转账记录
        //***************//

        //添加明细记录 用户出账
        TransactionEntity out = new TransactionEntity();
        out.setNumb(numb);
        out.setRemark("消费积分转账");
        out.setTType(9);
        out.setCreateDate(new Timestamp(new Date().getTime()));
        out.setModifyDate(new Timestamp(new Date().getTime()));
        out.setRecordType(0);
        out.setInoutType(0);
        out.setUserId(userEntity.getUserId());
        out.setAccount(userEntity.getAccount());
        transactionMapper.insert(out);

        //添加明细记录 用户出账手续费
//        TransactionEntity sx = new TransactionEntity();
//        sx.setNumb(B5.multiply(oneh).intValue());
//        sx.setRemark("消费积分转账手续费");
//        sx.setTType(10);
//        sx.setCreateDate(new Timestamp(new Date().getTime()));
//        sx.setModifyDate(new Timestamp(new Date().getTime()));
//        sx.setRecordType(0);
//        sx.setInoutType(0);
//        sx.setUserId(userEntity.getUserId());
//        sx.setAccount(userEntity.getAccount());
//        transactionMapper.insert(sx);

        //添加明细记录 用户进账
        TransactionEntity in = new TransactionEntity();
        in.setNumb(numb);
        in.setRemark("消费积分转账");
        in.setTType(9);
        in.setCreateDate(new Timestamp(new Date().getTime()));
        in.setModifyDate(new Timestamp(new Date().getTime()));
        in.setRecordType(0);
        in.setInoutType(1);
        in.setUserId(toUser.getUserId());
        in.setAccount(toUser.getAccount());
        transactionMapper.insert(in);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void psTransfer(String account, String mobile, Integer numb, String payPwd, String vcode) {
        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        if (!redisService.hasKey(ConstantInterface.VERIFICATION_CODE_FOR_PS + userEntity.getMobile()) || !vcode.equals(redisService.getString(ConstantInterface.VERIFICATION_CODE_FOR_PS + userEntity.getMobile()))){
            throw new ApiException(ErrorCodeEnum.TK1015.code(),ErrorCodeEnum.TK1015.msg());//转ps需要短信验证
        }

        payPwd = MD5Util.MD5Encode(payPwd,"utf-8");

        if (!payPwd.equals(userEntity.getPayPwd())){
            throw  new ApiException(ErrorCodeEnum.SW52.code(),ErrorCodeEnum.SW52.msg());
        }

        if (mobile.equals(userEntity.getMobile())){
            throw  new ApiException(ErrorCodeEnum.SW54.code(),ErrorCodeEnum.SW54.msg());
        }

        //扣减 申请转账用户
        UserPackEntity frompack = new UserPackEntity();
        frompack.setUserId(userEntity.getUserId());
        frompack = userPackMapper.selectOne(frompack);
        if (numb > frompack.getFreeIntegral()){
            throw new ApiException(ErrorCodeEnum.SW302.code(),ErrorCodeEnum.SW302.msg());
        }
        frompack.setFreeIntegral(frompack.getFreeIntegral() - numb);
        userPackMapper.updateByPrimaryKey(frompack);

        //扣减手续费
        SpecialEntity specialEntity = new SpecialEntity();
        specialEntity.setSpKey("minerFee");
        specialEntity = specialMapper.selectOne(specialEntity);

        BigDecimal bdnumb = new BigDecimal(numb.toString());
        BigDecimal spv = new BigDecimal(specialEntity.getSpValue());
        BigDecimal oneh = new BigDecimal(100);
        BigDecimal B3 = spv.divide(oneh, 2, RoundingMode.HALF_UP);//扣除比例
        BigDecimal B4 = bdnumb.divide(oneh,2,RoundingMode.HALF_UP);//被扣额度
        BigDecimal B5 = B4.multiply(B3).setScale(2,BigDecimal.ROUND_HALF_UP);//扣除额度
        BigDecimal B6 = B4.subtract(B5).setScale(2,BigDecimal.ROUND_HALF_UP).multiply(oneh);//扣除后剩余额度 * 100
        Integer B7 = B6.intValue();
        log.info("扣除比例：{}，额度：{}，扣除额度：{}，扣除后剩余额度(*100):{}, 取intValue：{}",B3, B4, B5, B6,B7);
        //增加 被转账用户 的ps
        UserEntity toUser = new UserEntity();
        toUser.setMobile(mobile);
        toUser = userMapper.selectOne(toUser);
        if(toUser == null){
            throw new ApiException(ErrorCodeEnum.PARAM_200026);
        }
        UserPackEntity topack = new UserPackEntity();
        topack.setUserId(toUser.getUserId());
        topack = userPackMapper.selectOne(topack);
        topack.setFreeIntegral(topack.getFreeIntegral() + B7);
        userPackMapper.updateByPrimaryKey(topack);

        //添加ps转账记录
        PSTransferEntity psTransferEntity = new PSTransferEntity();
        psTransferEntity.setFromUserId(userEntity.getUserId());
        psTransferEntity.setNumb(B7);
        psTransferEntity.setToUserId(toUser.getUserId());
        psTransferEntity.setCreateDate(new Timestamp(new Date().getTime()));
        psTransferEntity.setModifyDate(new Timestamp(new Date().getTime()));
        psTransferMapper.insert(psTransferEntity);

        //添加明细记录 用户出账
        TransactionEntity out = new TransactionEntity();
        out.setNumb(B7);
        out.setRemark("PS转账");
        out.setTType(4);
        out.setCreateDate(new Timestamp(new Date().getTime()));
        out.setModifyDate(new Timestamp(new Date().getTime()));
        out.setRecordType(0);
        out.setInoutType(0);
        out.setUserId(userEntity.getUserId());
        out.setAccount(userEntity.getAccount());
        transactionMapper.insert(out);

        //ps转账手续费
        TransactionEntity sx = new TransactionEntity();
        sx.setNumb(B5.multiply(oneh).intValue());
        sx.setRemark("PS转账手续费");
        sx.setTType(10);
        sx.setCreateDate(new Timestamp(new Date().getTime()));
        sx.setModifyDate(new Timestamp(new Date().getTime()));
        sx.setRecordType(0);
        sx.setInoutType(0);
        sx.setUserId(userEntity.getUserId());
        sx.setAccount(userEntity.getAccount());
        transactionMapper.insert(sx);

        //添加明细记录 用户进账
        TransactionEntity in = new TransactionEntity();
        in.setNumb(B7);
        in.setRemark("PS转账");
        in.setTType(4);
        in.setCreateDate(new Timestamp(new Date().getTime()));
        in.setModifyDate(new Timestamp(new Date().getTime()));
        in.setRecordType(0);
        in.setInoutType(1);
        in.setUserId(toUser.getUserId());
        in.setAccount(toUser.getAccount());
        transactionMapper.insert(in);

    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void sendVerificationCodeForPS(String account) {

        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        String mobile = userEntity.getMobile();
        String vcode = String.valueOf((int)(Math.random()*(9999-1000+1)+1000));
        redisService.setString(ConstantInterface.VERIFICATION_CODE_FOR_PS + mobile, vcode, 1, TimeUnit.MINUTES);
        SendSms.send(vcode,mobile);

//        String res = SendSms.send(vcode, mobile);
//        log.info("转账ps验证码res：{}" , res);
//        JSONObject jsonres = JSONObject.parseObject(res);
//        log.info("转账ps验证码jsonres：{}" , jsonres);
//
//        if (!"OK".equals(jsonres.getString("Code"))){
//            throw new ApiException(ErrorCodeEnum.PARAM_200012.code(),ErrorCodeEnum.PARAM_200012.msg());
//        }

    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public PageResult<List<TransactionVo>> getTrans(String account, Integer recordType, Integer pageSize,Integer rows) {

        PageResult list=new PageResult();
        Example example=new Example(TransactionEntity.class);
        example.setOrderByClause("id desc");
        Example.Criteria criteria = example.createCriteria();
        if(account!=null)
            criteria.andEqualTo("account", account);
        if(recordType!=null && !recordType.equals(0))
            criteria.andEqualTo("recordType", recordType);

        RowBounds rowBounds = RowBoundUtil.getRowBounds(rows, pageSize);

//        TransactionEntity transactionEntity = new TransactionEntity();
//        transactionEntity.setAccount(account);
//        if (!recordType.equals(0)){
//            transactionEntity.setRecordType(recordType);
//        }

        List<TransactionEntity> transactionEntityList = transactionMapper.selectByExampleAndRowBounds(example, rowBounds);
        List<TransactionVo> vos = new ArrayList<>();
        for (TransactionEntity entity : transactionEntityList) {
            TransactionVo transaction = new TransactionVo();
            transaction.setModifyDate(entity.getModifyDate());
            transaction.setCreateDate(entity.getCreateDate());
            transaction.setRemark(entity.getRemark());
            transaction.setTransacationType(entity.getTType());
            transaction.setRecordType(entity.getRecordType());
            transaction.setAccount(entity.getAccount());
            transaction.setInoutType(entity.getInoutType());
            transaction.setUserId(entity.getUserId());
            BigDecimal nubm = new BigDecimal(entity.getNumb());
            BigDecimal oneh = new BigDecimal(100);
            transaction.setNumb(String.valueOf(nubm.divide(oneh,2, RoundingMode.HALF_UP)));
            vos.add(transaction);
        }

        list.setData(vos);
        list.setTotal(transactionMapper.selectCountByExample(example));

        return list;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<SpecialVo> getPzshMiner() {
        List<SpecialEntity> list = specialMapper.selectAll();
        List<SpecialVo> specialEntities = new ArrayList<>();
        for (int i =0;i<list.size();i++){
            if ("minerFee".equals(list.get(i).getSpKey())){
                SpecialVo specialVo = new SpecialVo();
                specialVo.setId(list.get(i).getId());
                specialVo.setSpKey(list.get(i).getSpKey());
                specialVo.setSpValue(list.get(i).getSpValue());
                specialEntities.add(specialVo);
            }
            if ("pzshPrice".equals(list.get(i).getSpKey())){
                SpecialVo specialVo = new SpecialVo();
                specialVo.setId(list.get(i).getId());
                specialVo.setSpKey(list.get(i).getSpKey());

                BigDecimal spv = new BigDecimal(list.get(i).getSpValue());
                BigDecimal oneh = new BigDecimal(100);
                specialVo.setSpValue(String.valueOf(spv.divide(oneh, 2, RoundingMode.HALF_UP)));

                specialEntities.add(specialVo);
            }
        }
        return specialEntities;
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void RD2PS(String account) {

        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        UserPackEntity userPackEntity = new UserPackEntity();
        userPackEntity.setUserId(userEntity.getUserId());
        userPackEntity = userPackMapper.selectOne(userPackEntity);

        if (userEntity.getGrade() <= 0){
            throw new ApiException(ErrorCodeEnum.PARAM_200029);
        }

        String msg = "RD系统请求异常";
        try {
            //请求RD积分系统接口--获取会员信息
            Map<String, String> headers = new HashMap<String, String>();
            Map<String, String> querys = new HashMap<String, String>();
            Map<String, String> bodys = new HashMap<String, String>();
            querys.put("username", userEntity.getAccount());
            Integer wallet = 0;
            HttpResponse response = HttpUtils.doGet(EOLINKERConstants.DOMAIN_URL,EOLINKERConstants.USER_INFO, RequestMethod.GET.name(),headers,querys);
            String str = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSONObject.parseObject(str);
            Integer status = jsonObject.getInteger("status");
            msg = jsonObject.getString("msg");
            if (status.equals(0)){
                wallet = (int)(jsonObject.getJSONObject("data").getDouble("wallet_2") * 100);
                if (wallet <= 0){
                    throw new ApiException(ErrorCodeEnum.SW302);
                }
            }else{
                log.error("RD系统获取会员信息失败：{}", str);
                throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(), msg));
            }
            userPackEntity.setRdIntegral(0);// 清空 RD

            SpecialEntity specialEntity = new SpecialEntity();
            specialEntity.setSpKey("rd2ps");
            specialEntity = specialMapper.selectOne(specialEntity);

            //RD兑换需要按比例扣手续费
            BigDecimal wb = new BigDecimal(BigDecimalUtil.caseString_2(wallet));
            BigDecimal sb = new BigDecimal(BigDecimalUtil.caseString_2(Integer.valueOf(specialEntity.getSpValue())));
            BigDecimal nb = wb.multiply(sb).setScale(2,RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            Integer inb = nb.intValue();
            log.info("RD数量：{}，手续费比例：{}，扣除数量：{}，intValue：{}", wb, sb, nb, inb);
            Integer act = wallet - inb;
            if (act <= 0){
                throw new ApiException(ErrorCodeEnum.PARAM_200028);
            }

            userPackEntity.setFreeIntegral(userPackEntity.getFreeIntegral() + act);
            userPackMapper.updateByPrimaryKey(userPackEntity);

            //添加明细记录 用户RD出账
            TransactionEntity out = new TransactionEntity();
            out.setNumb(act);
            out.setRemark("扣减RD(RD兑换PS)");
            out.setTType(6);
            out.setCreateDate(new Timestamp(new Date().getTime()));
            out.setModifyDate(new Timestamp(new Date().getTime()));
            out.setRecordType(1);
            out.setInoutType(0);
            out.setUserId(userEntity.getUserId());
            out.setAccount(userEntity.getAccount());
            transactionMapper.insert(out);


            //添加明细记录 用户RD手续费 出账
            TransactionEntity sx = new TransactionEntity();
            sx.setNumb(inb);
            sx.setRemark("手续费(RD兑换PS)");
            sx.setTType(13);
            sx.setCreateDate(new Timestamp(new Date().getTime()));
            sx.setModifyDate(new Timestamp(new Date().getTime()));
            sx.setRecordType(1);
            sx.setInoutType(0);
            sx.setUserId(userEntity.getUserId());
            sx.setAccount(userEntity.getAccount());
            transactionMapper.insert(sx);

            //添加明细记录 用户PS进账
            TransactionEntity in = new TransactionEntity();
            in.setNumb(act);
            in.setRemark("PS进账(RD兑换PS)");
            in.setTType(6);
            in.setCreateDate(new Timestamp(new Date().getTime()));
            in.setModifyDate(new Timestamp(new Date().getTime()));
            in.setRecordType(1);
            in.setInoutType(1);
            in.setUserId(userEntity.getUserId());
            in.setAccount(userEntity.getAccount());
            transactionMapper.insert(in);


            //扣减 RD
            Map<String, String> header1 = new HashMap<String, String>();
            Map<String, String> query1 = new HashMap<String, String>();
            Map<String, String> body1 = new HashMap<String, String>();
            body1.put("username", userEntity.getAccount());
            body1.put("wallet", "2");
            body1.put("money", jsonObject.getJSONObject("data").getString("wallet_2"));
            body1.put("type", "2");
            HttpResponse response1 = HttpUtils.doPost(EOLINKERConstants.DOMAIN_URL,EOLINKERConstants.WALLET_CHANGE, RequestMethod.POST.name(),header1,query1,body1);
            String str1 = EntityUtils.toString(response1.getEntity());
            log.info("扣减RD 返回：{}", str1);
            JSONObject jsonObject1 = JSONObject.parseObject(str1);
            Integer status1 = jsonObject1.getInteger("status");
            msg = jsonObject1.getString("msg");
            if (status1.equals(0)){

            }else{
                log.error("扣减RD失败：{}", str1);
                throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(), msg));
            }
        } catch (Exception e) {
            log.error("RD兑换失败");
            throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(), msg));
        }

    }

}
